package com.yby.mall.product.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.yby.common.utils.PageUtils;
import com.yby.common.utils.R;
import com.yby.mall.product.dao.AttrAttrgroupRelationDao;
import com.yby.mall.product.dao.AttrDao;
import com.yby.mall.product.dao.CategoryDao;
import com.yby.mall.product.dto.AttrAttrgroupRelationDto;
import com.yby.mall.product.dto.AttrDto;
import com.yby.mall.product.dto.AttrGroupWithAttrDto;
import com.yby.mall.product.entity.*;
import com.yby.mall.product.service.AttrAttrgroupRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yby.common.utils.Query;

import com.yby.mall.product.dao.AttrGroupDao;
import com.yby.mall.product.service.AttrGroupService;
import org.springframework.util.StringUtils;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrGroupDao attrGroupDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    @Autowired
    private AttrDao attrDao;

    @Override
    public R queryPage(Integer catId, Integer page, Integer limit, String key) {
        PageHelper.startPage(page, limit);
        List<AttrGroupEntity> list = attrGroupDao.selectList(Wrappers.<AttrGroupEntity>lambdaQuery()
                .eq(catId != 0, AttrGroupEntity::getCatelogId, catId)
                .like(!StringUtils.isEmpty(key), AttrGroupEntity::getAttrGroupName, key));
        return R.ok().put("page", PageUtils.restPage(list));
    }

    @Override
    public R getInfo(Long attrGroupId) {
        AttrGroupEntity attrGroupEntity = attrGroupDao.selectById(attrGroupId);
        List<Long> list = new ArrayList<>();
        if(attrGroupEntity.getCatelogId() != null){
            findByParentId(attrGroupEntity.getCatelogId(), list);
        }
        attrGroupEntity.setCatelogPath(list);
        return R.ok().put("attrGroup",attrGroupEntity);
    }

    @Override
    public R attrRelationList(Long attrGroupId) {
        List<AttrAttrgroupRelationDto> list = attrAttrgroupRelationDao.attrRelationList(attrGroupId);
        return R.ok().put("data",list);
    }

    @Override
    public R queryPageNoAttr(Long attrGroupId, Integer page, Integer limit, String key) {
        PageHelper.startPage(page, limit);
        List<AttrDto> list = attrGroupDao.queryPageNoAttr(attrGroupId,key);
        return R.ok().put("page", PageUtils.restPage(list));
    }

    @Override
    public R deleteAttrRelation(List<Map<String, Object>> list) {
        attrAttrgroupRelationDao.deleteAttrRelation(list);
        return R.ok();
    }

    @Override
    public R insertAttrRelation(List<Map<String, Object>> list) {
        for (Map<String, Object> map : list) {
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            entity.setAttrId(Long.valueOf(map.get("attrId").toString()));
            entity.setAttrGroupId(Long.valueOf(map.get("attrGroupId").toString()));
            attrAttrgroupRelationDao.insert(entity);
        }
        return R.ok();
    }

    @Override
    public R withAttrList(Long catalogId) {
        List<AttrGroupWithAttrDto> list = attrGroupDao.withAttrList(catalogId);

        return R.ok().put("data",list);
    }

    public List<Long> findByParentId(Long id, List<Long> list) {
        list.add(id);
        CategoryEntity categoryEntity = categoryDao.selectById(id);
        if (categoryEntity.getParentCid() != 0) {
            findByParentId(categoryEntity.getParentCid(),list);
        }
        Collections.reverse(list);
        return list;
    }
}